package top.soulblack.purchasesale.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.soulblack.purchasesale.mapper.RoleMapper;
import top.soulblack.purchasesale.mapper.SpikeUserMapper;
import top.soulblack.purchasesale.model.Role;
import top.soulblack.purchasesale.model.SpikeUser;

import java.io.Console;
import java.util.List;

@Service
public class RoleService {
    @Autowired
    private RoleMapper roleMapper;
    private SpikeUser spikeUser;
    private String error;

    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }

    public String getError(){
        return error;
    }

    public boolean existRole(String name) {
        int num  = roleMapper.existrole(name);
        if (num == 1) {
            return true;
        }
        return false;
    }

    public boolean updateRole(String name,String oldname){
        int num=roleMapper.updateRole(name,oldname);
        if(num==1){
            return true;
        }
        return false;
    }

    public void addRole(String role) {
        roleMapper.addRole(role);
        return;
    }

    public boolean deleteRole(int roleId) {
        int num_user = roleMapper.userole(roleId);
        if(num_user>=1)
            return false;
        int num = roleMapper.deleteID(roleId);
        if (num >= 1) {
            return true;
        }
        return false;
    }

    public boolean copyasNew(String rolename) {
        int num = roleMapper.copyasNew(rolename);
        if (num == 1) {
            return true;
        }
        return false;
    }


    public int insert(){
        Role role = new Role();
        role.setName("test");
        return roleMapper.insert(role);
    }

    public String getNickname() {

        return spikeUser.getNickname();
    }
}
